import {Directive, HostListener,ElementRef} from '@angular/core';
import {Platform} from 'ionic-angular';

/**
 * Generated class for the InertiaScrollDirective directive.
 *
 * See https://angular.io/api/core/Directive for more info on Angular
 * Directives.
 */
@Directive({
    selector: '[inertia-scroll]' // Attribute selector
})
export class InertiaScrollDirective {
    // 纵向起点
    private startY:number;
    // 纵向止点
    private endY:number;
    // 起点时间
    private startTime:number;
    // 止点时间
    private endTime:number;
    // 速度衰减量
    private speedDecay:number=0.02;
    // 最后一次手指停止移动时间
    private lastMoveTime:number;
    // 倒数第二次手指停止时间
    private secondLastMoveTime:number;
    // 手指停止滑动时间
    private stopMoveInterval:any;
    // 停止惯性滚动
    private stopInertiaMove:boolean=false;

    // isIos
    private isIos:boolean;


    // touchstart
    @HostListener('touchstart', ['$event'])
    onTouchstart(event:HTMLDivElement):void{
        this.calcStart(event);
    }

    // touchmove
    @HostListener('touchmove', ['$event'])
    onTouchmove(event:HTMLDivElement):void{
        this.calcMove(event);
    }

    // touchend
    @HostListener('touchend', ['$event'])
    onTouchend(event:HTMLDivElement):void{
        this.calcEnd(event);
    }

    constructor(private elementRef:ElementRef,private platform:Platform) {
        this.isIos=this.platform.is('ios');
    }

    // touchstart计算
    calcStart(event):void{
        if(!this.isIos){return}
        this.stopInertiaMove = true;
        this.startY = event.touches[0].pageY;
        this.startTime = Date.now();
    }

    // touchmove计算
    calcMove(event):void{
        if(!this.isIos){return}
        this.secondLastMoveTime = this.lastMoveTime;
        this.lastMoveTime = Date.now();
    }

    // touchend计算
    calcEnd(event):void{
        if(!this.isIos){return}
        this.endY = event.changedTouches[0].pageY;
        this.endTime = Date.now();
        if (this.secondLastMoveTime) {
            this.stopMoveInterval = this.endTime - this.secondLastMoveTime;
        } else {
            this.stopMoveInterval = this.endTime - this.lastMoveTime;
        }

        let speed=(this.endY-this.startY)/(this.endTime-this.startTime);
        let speedAbs=Math.abs(speed);


        if (this.stopMoveInterval < 100) {
            //手指停止滑动超过0.1秒的就不执行惯性滑动了
            this.stopInertiaMove = false;
            this.calcLoop(event,speed,speedAbs);
        }
    }

    // calcLoop每次touchEnd后计算
    calcLoop(event,speed,speedAbs):void{
        if (speedAbs < 0 || this.stopInertiaMove) {
            //如果速度绝对值小于0了，则结束惯性滚动
            return;
        }


        //设置每次惯性滑动时间为20毫秒
        let distance = speedAbs * 20;


        if (speed < 0) {
            //如果速度是负数，则是手指向上滑动。继续惯性滚动，则scrollTop的值会增加
            event.scrollTop += distance;
            //console.log('向上惯性滚动' + distance);
        } else {
            //如果速度是正数，则是手指向下滑动。继续惯性滚动，则scrollTop的值会减少
            event.scrollTop -= distance;
            //console.log('向下惯性滚动' + distance);
        }

        //速度衰减
        speedAbs -= this.speedDecay;

        setTimeout(this.calcLoop(event,speed,speedAbs), 10);
    }

}
